package com.shop.api;

import com.shop.api.params.UserLoginParam;
import com.shop.api.params.UserRegisterParam;
import com.shop.api.params.UserUpdateParam;
import com.shop.config.annotation.TokenToUser;
import com.shop.entity.User;
import com.shop.service.UserService;
import com.shop.util.Result;
import com.shop.util.ResultGenerator;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;

@Slf4j
@RestController
@RequestMapping("/api/user")
public class UserApi {

    @Resource
    private UserService userService;

    //@CrossOrigin
    @PostMapping("/login")
    public Result login(@RequestBody @Valid UserLoginParam userLoginParam) {

        String loginResult = userService.login(userLoginParam.getUsername(), userLoginParam.getPassword());

        log.info("loginApi,登录名={},loginResult={}", userLoginParam.getUsername(), loginResult);

        //登录成功
        if (!StringUtils.isEmpty(loginResult) && loginResult.length() == 32) {
            Result result = ResultGenerator.genSuccessResult(loginResult);
            return result;
        }
        //登录失败
        return ResultGenerator.genFailResult(Integer.parseInt(loginResult));
    }


    @PostMapping("/logout")
    public Result logout(@TokenToUser User user) {
        if (user == null) {
            return ResultGenerator.genFailResult(50003);// token失效
        }
        Boolean logoutResult = userService.logout(user.getUserId());
        log.info("loginApi,loginUser={}", user.getUsername());
        //登出成功
        if (logoutResult) {
            return ResultGenerator.genSuccessResult();
        }
        //登出失败
        return ResultGenerator.genFailResult(50006);//登出失败
    }

    @PostMapping("/register")
    public Result register(@RequestBody @Valid UserRegisterParam userRegisterParam) {
        int registerResult = userService.register(userRegisterParam.getUsername(), userRegisterParam.getPassword());

        log.info("registerApi,loginName={},loginResult={}", userRegisterParam.getUsername(), registerResult);

        //注册成功
        if (registerResult == 20000) {
            return ResultGenerator.genSuccessResult();
        }
        //注册失败
        return ResultGenerator.genFailResult(registerResult);
    }

    @PostMapping("/update")
    public Result updataUser(@RequestBody UserUpdateParam userUpdateParam, @TokenToUser User loginUser) {
        if (loginUser == null) {
            return ResultGenerator.genFailResult(50003);//token失效
        }
        Boolean flag = userService.updatePassword(loginUser.getUserId()
                , userUpdateParam.getOriginalPassword(), userUpdateParam.getNewPassword());
        if (flag) {
            //返回成功
            log.info("updateApi,{}修改了密码", loginUser.getUsername());
            return ResultGenerator.genSuccessResult();
        } else {
            //返回失败
            return ResultGenerator.genFailResult(50005);//密码修改失败
        }
    }

    @PostMapping("/toSeller")
    public Result toSeller(@TokenToUser User loginUser) {
        if (loginUser == null) {
            return ResultGenerator.genFailResult(50003);//token失效
        }

        int result = userService.toSeller(loginUser);
        if (result == 20000) {
            log.info("userApi,{}变成卖家", loginUser.getUsername());
            return ResultGenerator.genSuccessResult();
        }

        return ResultGenerator.genFailResult(result);
    }

    @PostMapping("/info")
    public Result info(@TokenToUser User loginUser) {
        if (loginUser == null) {
            return ResultGenerator.genFailResult(50003);//token失效
        }

        log.info("userApi,{}类型是{}", loginUser.getUsername(), loginUser.getUserType());
        HashMap map = new HashMap<>();
        map.put("userType", loginUser.getUserType());
        return ResultGenerator.genSuccessResult(map);

    }


}
